package util

import (
	"bytes"
	"fmt"
	"github.com/emirpasic/gods/utils"
	"io/ioutil"
	"net/http"
)

func PostJSON(url string, jsonBody string, header map[string]interface{}) (string, error) {
	var jsonStr = []byte(jsonBody)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", "application/json")
	if header != nil {
		for s := range header {
			req.Header.Set(s, utils.ToString(header[s]))
		}
	}
	client := http.DefaultClient
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	result := string(body)
	return result, nil
}

func PostJSONDebug(url string, jsonBody string, header map[string]interface{}) (string, error) {
	fmt.Println("请求路径：", url)
	fmt.Println("请求参数：", jsonBody)
	fmt.Println("请求头：", header)
	return PostJSON(url, jsonBody, header)
}
